/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package neo.workforce.dao.impl;

import java.sql.SQLException;
import java.util.List;
import neo.workforce.dao.Activity;
import neo.workforce.dao.ActivityDAO;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

/**
 *
 * @author krisada
 */
public class ActivityDAOImpl implements ActivityDAO {
    
    private HibernateTemplate hibernateTemplate;
    private Logger log = Logger.getLogger(this.getClass());

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public Activity find(long id) {
        return this.hibernateTemplate.get(Activity.class, new Long(id));
    }

    public List findByName(String activityName, final long processId) {
        StringBuilder buf = new StringBuilder();
        buf.append("FROM neo.workforce.dao.Activity o");
        if (activityName != null) {
            buf.append(" WHERE o.name like '%").append(activityName).append("%' desc o.createDate");
        }
        log.info(buf.toString());
        java.util.List ls = hibernateTemplate.find(buf.toString());
        return ls;
    }

    public List findByName(final int begin, final int limit, final String activityName, final long processId) {
        return getHibernateTemplate().executeFind(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                StringBuilder buf = new StringBuilder();
                buf.append("FROM neo.workforce.dao.Activity o");
                if (activityName != null) {
                    buf.append(" WHERE o.process.id = ").append(processId).append(" AND ");
                    buf.append("o.name like '%").append(activityName).append("%' desc o.createDate");
                }
                log.info(buf.toString());
                Query q = session.createQuery(buf.toString());
                q.setFirstResult(begin);
                q.setMaxResults(limit);
                return q.list();
            }
        });
    }

    public Long save(final Activity activity) {
        long lid = -1;
        HibernateCallback callback = new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {
                Long id = (Long) session.save(activity);
                activity.setId(new Long(id));
                return null;
            }
        };
        hibernateTemplate.execute(callback);
        return activity.getId();
    }

    public boolean update(final Activity activity) {
        try {
            HibernateCallback callback = new HibernateCallback() {
                public Object doInHibernate(Session session)
                        throws HibernateException, SQLException {
                    session.update(activity);
                    return null;
                }
            };
            hibernateTemplate.execute(callback);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public boolean remove(long id) {
        try {
            neo.workforce.dao.Activity activity = this.find(id);
            hibernateTemplate.delete(activity);
            return true;
        } catch(Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }
    
    
}
